<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="节点名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['nodename']" placeholder="请输入节点名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="审核类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select @change="change" v-decorator="['audittype']">
                <a-select-option value="指定人员"> 指定人员 </a-select-option>
                <a-select-option value="指定角色"> 指定角色 </a-select-option>
              </a-select>
            </a-form-item></a-col
          >
          <a-col :span="24">
            <a-form-item label="审核子类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select default-value="全部审核" v-decorator="['auditchildtype']">
                <a-select-option value="随机一个审核"> 随机一个审核 </a-select-option>
                <a-select-option value="任意一个审核"> 任意一个审核 </a-select-option>
                <a-select-option value="全部审核"> 全部审核 </a-select-option>
                <a-select-option value="产品指定审核">产品指定审核</a-select-option>
              </a-select>
            </a-form-item></a-col>
          <a-col :span="24" v-if="isshow == 1">
            <a-form-item label="审核人员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select placeholder="请选择审核人员" v-decorator="['by2']">
                <a-select-option v-for="(role, roleindex) in by2List" :key="roleindex.toString()" :value="role.id">
                  {{ role.realname }}
                </a-select-option>

              </a-select>
            </a-form-item>

          </a-col>

          <a-col :span="24" v-if="isshow == 2">
            <a-form-item label="审核角色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                style="width: 100%"
                placeholder="请选择用户角色"
                optionFilterProp="children"
                v-decorator="['by2']"
                :getPopupContainer="(target) => target.parentNode"
              >
                <a-select-option v-for="(role, roleindex) in roleList" :key="roleindex.toString()" :value="role.id">
                  {{ role.roleName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import { addUser, editUser, queryUserRole, queryall } from '@/api/api'

export default {
  name: 'TtCollectFlowNodeForm',
  components: {
    JFormContainer
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      isshow: '',
      by2List: [],
      roleList: [],
      selectedRole: [],
      flowcode: '',
      tjjb: '',
      sortid: '',

      url: {
        add: '/tt_question_collect/ttCollectFlowNode/add',
        edit: '/tt_question_collect/ttCollectFlowNode/edit',
        queryById: '/tt_question_collect/ttCollectFlowNode/queryById',
        flowUserList: '/sys/user/flowUserList'
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    // 如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
    this.initialRoleList()
  },
  methods: {
    change(val) {
      console.log(val)
      if (val == '指定角色') {
        this.isshow = 2
      } else {
        this.isshow = 1
        this.flowUserList()
      }
    },
    initialRoleList() {
      queryall().then((res) => {
        if (res.success) {
          this.roleList = res.result
        } else {
          console.log(res.message)
        }
      })
    },
    flowUserList() {
      getAction(this.url.flowUserList).then((res) => {
        if (res.success) {
          this.by2List = res.result
          console.log(res.result)
        }
      })
    },
    add(flowcode, tjjb, sortid) {
      this.flowcode = flowcode
      this.tjjb = tjjb
      this.sortid = sortid
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.change(record.audittype)
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'flowcode',
            'nodename',
            'audittype',
            'auditchildtype',
            'sortid',
            'auditid',
            'auditname',
            'shbj',
            'scbj',
            'by2'
          )
        )
      })
    },
    // 渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            this.model.flowcode = this.flowcode
            this.model.by1 = this.tjjb
            this.model.sortid = this.sortid
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          console.log('model', this.model)
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)

          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.$emit('child-event')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'flowcode',
          'nodename',
          'audittype',
          'auditchildtype',
          'sortid',
          'auditid',
          'auditname',
          'shbj',
          'scbj',
          'by2'
        )
      )
    }
  }
}
</script>
